package com.dcrzl.shcz.module.info.controller;

import java.util.*;

import com.dcrzl.shcz.common.enums.UserTypeEnum;
import com.dcrzl.shcz.common.utils.BaseController;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.module.info.service.GovUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.dcrzl.shcz.common.entity.info.GovUnit;

/**
 * 行政单位 财政局等
 *
 * @author dcrzl
 * @email 1728795786@qq.com
 * @date 2025-01-01 22:49:28
 */
@RestController
@RequestMapping("/gov/unit")
public class GovUnitController extends BaseController {
    @Autowired
    private GovUnitService govUnitService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result<List<GovUnit>> list(GovUnit govUnit) {
        if (Objects.equals(this.getUserType(), UserTypeEnum.WX_MINI_USER_TYPE.getCode())) {
            Collection<String> govUnitIdList = new ArrayList<>();
            Collection<String> govUnitIdList1 = this.getGovUnitIdList1();
            if(!CollectionUtils.isEmpty(govUnitIdList1)){
                govUnitIdList.addAll(govUnitIdList1);
            }
            Collection<String> govUnitIdList2 = this.getGovUnitIdList2();
            if(!CollectionUtils.isEmpty(govUnitIdList2)){
                govUnitIdList.addAll(govUnitIdList2);
            }
            govUnit.setIdListStr(govUnitIdList);
        }
        return govUnitService.selectList(govUnit);
    }

    /**
     * 列表分页
     */
    @GetMapping("/page")
    public PageInfo<GovUnit> page(GovUnit govUnit) {
        return govUnitService.selectUPage(govUnit);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public Result<GovUnit> info(@PathVariable("id") Long id) {
        return govUnitService.selectById(id);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody GovUnit govUnit) {
        return govUnitService.insert(govUnit);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result<Integer> update(@RequestBody GovUnit govUnit) {
        return govUnitService.updateById(govUnit);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result<Integer> delete(@RequestBody Long[] ids) {
        return govUnitService.deleteBatchIds(Arrays.asList(ids));
    }

}
